package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import ssm.pojo.User;
import ssm.service.CartService;

import javax.servlet.http.HttpSession;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加书籍
     * @param bookId
     * @param session
     * @return
     */
    @RequestMapping("/cart/{bookId}")
    public String addCart(@PathVariable("bookId") int bookId, HttpSession session){
        User user = cartService.addCart(bookId, session);
        session.setAttribute("user",user);
        return "redirect:/cart";
    }

    /**
     * 返回购物车
     * @return
     */
    @RequestMapping("/cart")
    public String cart(){
        return "/pages/cart/cart";
    }

    /**
     * 修改购物车内书籍数量
     * @param bookId
     * @param session
     * @return
     */
    @RequestMapping("/updateCartAdd/{bookId}")
    @ResponseBody
    public String updateCartAdd(@PathVariable("bookId") Integer bookId, HttpSession session){
        User user = cartService.updateCartAdd(bookId,session);
        session.setAttribute("user",user);
        return "";
    }

    /**
     * 修改购物车内书籍数量
     * @param bookId
     * @param session
     * @return
     */
    @RequestMapping("/updateCartMove/{bookId}")
    public String updateCartMove(@PathVariable("bookId") Integer bookId, HttpSession session){
        User user = cartService.updateCartMove(bookId,session);
        session.setAttribute("user",user);
        return "redirect:/cart";
    }

    @RequestMapping("cartInfo")
    @ResponseBody
    public User cartInfo(HttpSession session){
        User user = (User) session.getAttribute("user");
        return user;
    }


}
